package com.itfitness.openslesdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ResourceUtils;
import com.itfitness.openslesdemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
    private Button btAssets;
    private Button btUrl;
    private Button btPcm;
    private Button btPause;
    private int currentPlayType = 0;//当前播放的类型（1.Assets 2.Url 3.PCM）
    private boolean isPlaying = false;//是否正在播放
    private String pcmFilePath;//pcm文件路径
    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btAssets = (Button) findViewById(R.id.bt_assets);
        btUrl = (Button) findViewById(R.id.bt_url);
        btPcm = (Button) findViewById(R.id.bt_pcm);
        btPause = (Button) findViewById(R.id.bt_pause);
        //先将PCM数据拷贝到SD卡
        copyPcm2Sd();
        initListener();
    }

    private void copyPcm2Sd() {
        pcmFilePath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/pcmData.pcm";
        new Thread(()->{
            ResourceUtils.copyFileFromAssets("mydream.pcm",pcmFilePath);
        }).start();
    }

    private void initListener() {
        btAssets.setOnClickListener(v->{
            currentPlayType = 1;
            isPlaying = true;
            btPause.setText("暂停播放");
            playAssets(getAssets(), "mydream.m4a");
        });
        btUrl.setOnClickListener(v->{
            isPlaying = true;
            currentPlayType = 2;
            btPause.setText("暂停播放");
            playUrl("https://sharefs.ali.kugou.com/202110061758/dbdc2f041d023da2ad0750cb09c43239/G241/M05/1D/09/kYcBAF-OrrqAetTMAEA5laOdkUg776.mp3");
        });
        btPcm.setOnClickListener(v->{
            isPlaying = true;
            currentPlayType = 3;
            btPause.setText("暂停播放");
            playPCM(pcmFilePath);
        });
        btPause.setOnClickListener(v->{
            if(isPlaying){
                int result = pause(currentPlayType);
                if(result == 1){
                    isPlaying = false;
                    btPause.setText("继续播放");
                }
            }else {
                int result = resume(currentPlayType);
                if(result == 1){
                    isPlaying = true;
                    btPause.setText("暂停播放");
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseAudio();
    }

    /**
     * 播放Assets文件
     */
    public native void playAssets(AssetManager assetManager,String fileName);

    /**
     * 播放Url
     */
    public native void playUrl(String url);

    /**
     * 播放PCM
     */
    public native void playPCM(String filePath);

    /**
     * 暂停播放(返回值为1为成功，0为失败)
     */
    public native int pause(int type);
    /**
     * 继续播放(返回值为1为成功，0为失败)
     */
    public native int resume(int type);
    /**
     * 释放资源
     */
    public native void releaseAudio();
}